package org.homly.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.homly.model.Config;
import org.homly.sql.SQLTool;
import org.homly.tool.StringTool;

public class ProductService {

	@SuppressWarnings("unchecked")
	public List<HashMap<String, String>> getConfigs(HashMap para) throws Exception {

		List<HashMap<String, String>> configs = null;

		Short status = (Short) para.get("status");

		StringBuffer sqlSB = new StringBuffer();
		sqlSB.append(" SELECT * FROM config");

		if (status != null && "0".equals(status.toString())) {
			sqlSB.append(" WHERE config_status=0");
		} else if  (status != null && "1".equals(status.toString())) {
			sqlSB.append(" WHERE config_status=1");
		}

		sqlSB.append(" ORDER BY config_sort_no DESC");
		try {
			configs = SQLTool.query(sqlSB.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return configs;
	}

	public int getConfigTopSortNo() throws Exception {

		int topSortNo = 0;

		String sql = " SELECT (MAX(config_sort_no)+1) AS top_sort_no FROM config";
		List<HashMap<String, String>> result = SQLTool.query(sql);
		if (result.size() > 0) {
			String topSortNoS = result.get(0).get("top_sort_no");
			if (topSortNoS != null && StringTool.isNumber(topSortNoS)) {
				topSortNo = Integer.valueOf(topSortNoS);
			}
		}

		return topSortNo;
	}

	public int getConfigBottomSortNo() throws Exception {

		int bottomSortNo = 0;

		String sql = " SELECT (MIN(config_sort_no)-1) AS bottom_sort_no FROM config";
		List<HashMap<String, String>> result = SQLTool.query(sql);
		if (result.size() > 0) {
			String bottomSortNoS = result.get(0).get("bottom_sort_no");
			if (bottomSortNoS != null && StringTool.isNumber(bottomSortNoS)) {
				bottomSortNo = Integer.valueOf(bottomSortNoS);
			}
		}

		return bottomSortNo;
	}

	public void saveConfig(Config config) throws Exception {

		String sql = " INSERT config(config_no,config_name,config_is_required,config_sort_no,config_status,config_create_time) VALUES(?,?,?,?,?,?)";
		Object[] para = {config.getConfigNo(), config.getConfigName(), config.getConfigIsRequired(), getConfigBottomSortNo(),
				config.getConfigStatus(), new Date()};

		SQLTool.insert(sql, para);
	}

	@SuppressWarnings("unchecked")
	public HashMap<String, String> getConfig(HashMap para) throws Exception {

		HashMap<String, String> config = null;

		Integer id = (Integer) para.get("id");

		StringBuffer sqlSB = new StringBuffer();
		sqlSB.append(" SELECT * FROM config");

		if (id != null) {
			sqlSB.append(" WHERE config_id=").append(id);
		}

		sqlSB.append(" LIMIT 1");
		try {
			List<HashMap<String, String>> configs = null;
			configs = SQLTool.query(sqlSB.toString());
			if (configs != null && configs.size() > 0) {
				config = configs.get(0);
			} else {
				config = new HashMap();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return config;
	}

	public void updateConfig(Config config) throws Exception {

		String sql = " UPDATE config SET config_no=?,config_name=?,config_is_required=?,config_status=?,config_modify_time=? WHERE config_id=?";
		Object[] para = {config.getConfigNo(), config.getConfigName(), config.getConfigIsRequired(), config.getConfigStatus(),
				new Date(), config.getConfigId()};

		SQLTool.update(sql, para);
	}

	public void setConfigTop(Integer configId) throws Exception {

		String sql = " UPDATE config SET config_sort_no=? WHERE config_id=?";
		Object[] para = {getConfigTopSortNo(), configId};

		SQLTool.update(sql, para);
	}

	public void setConfigBottom(Integer configId) throws Exception {

		String sql = " UPDATE config SET config_sort_no=? WHERE config_id=?";
		Object[] para = {getConfigBottomSortNo(), configId};

		SQLTool.update(sql, para);
	}

	public static int getProductTopSortNo() {

		int topSortNo = 0;

		try {
			String sql = " SELECT (MAX(product_sort_no)+1) AS top_sort_no FROM product";
			List<HashMap<String, String>> result = SQLTool.query(sql);
			if (result.size() > 0) {
				String topSortNoS = result.get(0).get("top_sort_no");
				if (topSortNoS != null && StringTool.isNumber(topSortNoS)) {
					topSortNo = Integer.valueOf(topSortNoS);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return topSortNo;
	}

	public static int getProductBottomSortNo() {

		int bottomSortNo = 0;

		try {
			String sql = " SELECT (MIN(product_sort_no)-1) AS bottom_sort_no FROM product";
			List<HashMap<String, String>> result = SQLTool.query(sql);
			if (result.size() > 0) {
				String bottomSortNoS = result.get(0).get("bottom_sort_no");
				if (bottomSortNoS != null && StringTool.isNumber(bottomSortNoS)) {
					bottomSortNo = Integer.valueOf(bottomSortNoS);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bottomSortNo;
	}

	public static String getProductAppendPicUrlById(Integer papId) {

		String url = "";

		try {
			String sql = "SELECT product_append_pic_url FROM product_append_pic WHERE product_append_pic_id=" + papId;
			List<HashMap<String, String>> urlList = SQLTool.query(sql);
			if (urlList != null && urlList.size() > 0) {
				url = urlList.get(0).get("product_append_pic_url");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return url;
	}

	public static void deleteProductAppendPicById(Integer papId) {

		try {
			String sql = "DELETE FROM product_append_pic WHERE product_append_pic_id=?";
			Object[] para = {papId};
			SQLTool.update(sql, para);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public static List<HashMap<String, String>> getProducts(HashMap para) throws Exception {

		List<HashMap<String, String>> products = null;

		String productIdsS = (String) para.get("productIds");
		Short status = (Short) para.get("status");

		StringBuffer sqlSB = new StringBuffer();
		sqlSB.append(" SELECT * FROM product WHERE 1=1");

		if (productIdsS != null && !"".equals(productIdsS)) {
			sqlSB.append(" AND product_id IN (").append(productIdsS).append(")");
		}
		if (status != null && "0".equals(status.toString())) {
			sqlSB.append(" AND product_status=0");
		} else if  (status != null && "1".equals(status.toString())) {
			sqlSB.append(" AND product_status=1");
		}

		if (productIdsS != null && !"".equals(productIdsS)) {
			sqlSB.append(" ORDER BY CASE product_id");

			String[] productIdsSArray = productIdsS.split(",");
			for (int i = 0; i < productIdsSArray.length; i ++) {
				sqlSB.append(" WHEN ").append(productIdsSArray[i]).append(" THEN ").append(i);
			}

			sqlSB.append(" END");
		}

		try {
			products = SQLTool.query(sqlSB.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return products;
	}
}
